{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model with Binary Data\n",
    "\n",
    "Example testing a model which uses binary data\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cp ../../../proto/prediction.proto ./proto\n",
    "!cp -r ../../../proto/tensorflow/tensorflow .\n",
    "!python -m grpc.tools.protoc -I. --python_out=. --grpc_python_out=. ./proto/prediction.proto"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import requests\n",
    "import base64\n",
    "from proto import prediction_pb2\n",
    "from proto import prediction_pb2_grpc\n",
    "import grpc\n",
    "import numpy as np\n",
    "import pickle\n",
    "\n",
    "def get_payload():\n",
    "    array_2d = np.arange(9).reshape(3, 3)\n",
    "    print(array_2d)\n",
    "    bdata = pickle.dumps(array_2d)\n",
    "    return bdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rest_request_ambassador(deploymentName,request,endpoint=\"localhost:8003\"):\n",
    "    response = requests.post(\n",
    "                \"http://\"+endpoint+\"/seldon/\"+deploymentName+\"/api/v0.1/predictions\",\n",
    "                json=request)\n",
    "    print(response.status_code)\n",
    "    print(response.text)\n",
    "    return response.json()\n",
    "\n",
    "def grpc_request_ambassador(deploymentName,data,endpoint=\"localhost:8004\"):\n",
    "    request = prediction_pb2.SeldonMessage(binData = data)\n",
    "    channel = grpc.insecure_channel(endpoint)\n",
    "    stub = prediction_pb2_grpc.SeldonStub(channel)\n",
    "    metadata = [('seldon',deploymentName)]\n",
    "    response = stub.Predict(request=request,metadata=metadata)\n",
    "    return response\n",
    "\n",
    "def rest_request_docker(request,endpoint=\"localhost:5000\"):\n",
    "    response = requests.post(\n",
    "                \"http://\"+endpoint+\"/predict\",\n",
    "                 data={\"json\":json.dumps(request),\"isDefault\":True})\n",
    "    print(response.text)\n",
    "    return response.json()\n",
    "\n",
    "def grpc_request_docker(data,endpoint=\"localhost:5000\"):\n",
    "    request = prediction_pb2.SeldonMessage(binData = data)\n",
    "    channel = grpc.insecure_channel(endpoint)\n",
    "    stub = prediction_pb2_grpc.ModelStub(channel)\n",
    "    response = stub.Predict(request=request)\n",
    "    return response\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# REST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---> Installing application source...\n",
      "Build completed successfully\n"
     ]
    }
   ],
   "source": [
    "!s2i build -E environment_rest . seldonio/seldon-core-s2i-python36:0.4-SNAPSHOT model-with-bindata-rest:0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ffe70f6aea345e9eeab52842c5ada67fbc5cf5a22cc638ce162e96539d4b7d5f\r\n"
     ]
    }
   ],
   "source": [
    "!docker run --name \"model-with-bindata\" -d --rm -p 5000:5000 model-with-bindata-rest:0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "{\"binData\":\"gANjbnVtcHkuY29yZS5tdWx0aWFycmF5Cl9yZWNvbnN0cnVjdApxAGNudW1weQpuZGFycmF5CnEBSwCFcQJDAWJxA4dxBFJxBShLAUsDSwOGcQZjbnVtcHkKZHR5cGUKcQdYAgAAAGk4cQhLAEsBh3EJUnEKKEsDWAEAAAA8cQtOTk5K/////0r/////SwB0cQxiiUNIAAAAAAAAAAABAAAAAAAAAAIAAAAAAAAAAwAAAAAAAAAEAAAAAAAAAAUAAAAAAAAABgAAAAAAAAAHAAAAAAAAAAgAAAAAAAAAcQ10cQ5iLg==\",\"meta\":{\"metrics\":[{\"key\":\"mycounter\",\"type\":\"COUNTER\",\"value\":1},{\"key\":\"mygauge\",\"type\":\"GAUGE\",\"value\":100},{\"key\":\"mytimer\",\"type\":\"TIMER\",\"value\":20.2}]}}\n",
      "\n",
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "(3, 3)\n"
     ]
    }
   ],
   "source": [
    "bdata = get_payload()\n",
    "bdata_base64 = base64.b64encode(bdata).decode('utf-8')\n",
    "payload = {\"meta\":{},\"binData\":bdata_base64}\n",
    "response = rest_request_docker(payload)\n",
    "bdata2 = base64.b64decode(response[\"binData\"])\n",
    "arr_resp = pickle.loads(bdata2)\n",
    "print(arr_resp)\n",
    "print(arr_resp.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model-with-bindata\r\n"
     ]
    }
   ],
   "source": [
    "!docker rm model-with-bindata --force"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# gRPC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---> Installing application source...\n",
      "Build completed successfully\n"
     ]
    }
   ],
   "source": [
    "!s2i build -E environment_grpc . seldonio/seldon-core-s2i-python36:0.4-SNAPSHOT model-with-bindata-grpc:0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "02365f08b326f43f72576d20b9cfb4ed71c1e404edaccbe7396d98e5bef7e30e\r\n"
     ]
    }
   ],
   "source": [
    "!docker run --name \"model-with-bindata\" -d --rm -p 5000:5000 model-with-bindata-grpc:0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = get_payload()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "(3, 3)\n"
     ]
    }
   ],
   "source": [
    "bdata = get_payload()\n",
    "resp = grpc_request_docker(bdata)\n",
    "bdata2 = resp.binData\n",
    "arr_resp = pickle.loads(bdata2)\n",
    "print(arr_resp)\n",
    "print(arr_resp.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model-with-bindata\r\n"
     ]
    }
   ],
   "source": [
    "!docker rm model-with-bindata --force"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test using Minikube\n",
    "\n",
    "**Due to a [minikube/s2i issue](https://github.com/SeldonIO/seldon-core/issues/253) you will need [s2i >= 1.1.13](https://github.com/openshift/source-to-image/releases/tag/v1.1.13)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting local Kubernetes v1.10.0 cluster...\n",
      "Starting VM...\n",
      "Getting VM IP address...\n",
      "Moving files into cluster...\n",
      "Setting up certs...\n",
      "Connecting to cluster...\n",
      "Setting up kubeconfig...\n",
      "Starting cluster components...\n",
      "^C\n"
     ]
    }
   ],
   "source": [
    "!minikube start --vm-driver kvm2 --memory 4096 --feature-gates=CustomResourceValidation=true --extra-config=apiserver.Authorization.Mode=RBAC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "clusterrolebinding.rbac.authorization.k8s.io/kube-system-cluster-admin created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create clusterrolebinding kube-system-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$HELM_HOME has been configured at /home/clive/.helm.\n",
      "\n",
      "Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.\n",
      "\n",
      "Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.\n",
      "To prevent this, run `helm init` with the --tiller-tls-verify flag.\n",
      "For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation\n",
      "Happy Helming!\n"
     ]
    }
   ],
   "source": [
    "!helm init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waiting for deployment \"tiller-deploy\" rollout to finish: 0 of 1 updated replicas are available...\n",
      "deployment \"tiller-deploy\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/tiller-deploy -n kube-system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   seldon-core-crd\n",
      "LAST DEPLOYED: Fri Nov 30 19:34:07 2018\n",
      "NAMESPACE: default\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1beta1/Deployment\n",
      "NAME                        DESIRED  CURRENT  UP-TO-DATE  AVAILABLE  AGE\n",
      "seldon-spartakus-volunteer  1        0        0           0          0s\n",
      "\n",
      "==> v1/ServiceAccount\n",
      "NAME                        SECRETS  AGE\n",
      "seldon-spartakus-volunteer  1        0s\n",
      "\n",
      "==> v1beta1/ClusterRole\n",
      "NAME                        AGE\n",
      "seldon-spartakus-volunteer  0s\n",
      "\n",
      "==> v1beta1/ClusterRoleBinding\n",
      "NAME                        AGE\n",
      "seldon-spartakus-volunteer  0s\n",
      "\n",
      "==> v1/ConfigMap\n",
      "NAME                     DATA  AGE\n",
      "seldon-spartakus-config  3     0s\n",
      "\n",
      "==> v1beta1/CustomResourceDefinition\n",
      "NAME                                         AGE\n",
      "seldondeployments.machinelearning.seldon.io  0s\n",
      "\n",
      "\n",
      "NOTES:\n",
      "NOTES: TODO\n",
      "\n",
      "\n",
      "NAME:   seldon-core\n",
      "LAST DEPLOYED: Fri Nov 30 19:34:07 2018\n",
      "NAMESPACE: default\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1beta1/RoleBinding\n",
      "NAME        AGE\n",
      "ambassador  1s\n",
      "\n",
      "==> v1beta1/ClusterRole\n",
      "NAME                AGE\n",
      "seldon-crd-default  1s\n",
      "\n",
      "==> v1beta1/Role\n",
      "ambassador    1s\n",
      "seldon-local  1s\n",
      "\n",
      "==> v1/RoleBinding\n",
      "NAME    AGE\n",
      "seldon  1s\n",
      "\n",
      "==> v1beta1/Deployment\n",
      "NAME                                DESIRED  CURRENT  UP-TO-DATE  AVAILABLE  AGE\n",
      "seldon-core-ambassador              1        1        1           0          0s\n",
      "seldon-core-seldon-apiserver        1        1        1           0          0s\n",
      "seldon-core-seldon-cluster-manager  1        1        1           0          0s\n",
      "seldon-core-redis                   1        1        1           0          0s\n",
      "\n",
      "==> v1/Pod(related)\n",
      "NAME                                                 READY  STATUS             RESTARTS  AGE\n",
      "seldon-core-ambassador-56cb8fc595-74cbt              0/1    ContainerCreating  0         0s\n",
      "seldon-core-seldon-apiserver-54488fb66b-kc5t9        0/1    ContainerCreating  0         0s\n",
      "seldon-core-seldon-cluster-manager-6c6cdd889d-47m7l  0/1    ContainerCreating  0         0s\n",
      "seldon-core-redis-78bb97f8c4-f6k29                   0/1    ContainerCreating  0         0s\n",
      "\n",
      "==> v1/ServiceAccount\n",
      "NAME    SECRETS  AGE\n",
      "seldon  1        1s\n",
      "\n",
      "==> v1/ClusterRoleBinding\n",
      "NAME            AGE\n",
      "seldon-default  1s\n",
      "\n",
      "==> v1/Service\n",
      "NAME                          TYPE       CLUSTER-IP     EXTERNAL-IP  PORT(S)                        AGE\n",
      "seldon-core-ambassador-admin  NodePort   10.107.87.39   <none>       8877:30901/TCP                 1s\n",
      "seldon-core-ambassador        NodePort   10.97.188.133  <none>       80:30178/TCP,443:32325/TCP     0s\n",
      "seldon-core-seldon-apiserver  NodePort   10.98.119.196  <none>       8080:32735/TCP,5000:31943/TCP  0s\n",
      "seldon-core-redis             ClusterIP  10.98.79.212   <none>       6379/TCP                       0s\n",
      "\n",
      "\n",
      "NOTES:\n",
      "Thank you for installing Seldon Core.\n",
      "\n",
      "Documentation can be found at https://github.com/SeldonIO/seldon-core\n",
      "\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!helm install ../../../helm-charts/seldon-core-crd --name seldon-core-crd  --set usage_metrics.enabled=true\n",
    "!helm install ../../../helm-charts/seldon-core --name seldon-core --set ambassador.enabled=true"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# REST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---> Installing application source...\n",
      "Build completed successfully\n"
     ]
    }
   ],
   "source": [
    "!eval $(minikube docker-env) && s2i build -E environment_rest . seldonio/seldon-core-s2i-python36:0.4-SNAPSHOT model-with-bindata-rest:0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "seldondeployment.machinelearning.seldon.io/mymodel created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f deployment-rest.json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wait until ready (replicas == replicasAvailable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "map[predictorStatus:[map[name:mymodel-mymodel-b0e3779 replicas:1 replicasAvailable:0]] state:Creating]"
     ]
    }
   ],
   "source": [
    "!kubectl get seldondeployments mymodel -o jsonpath='{.status}' "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "minikube_ip = !minikube ip\n",
    "minikube_port = !kubectl get svc -l service=ambassador -o jsonpath='{.items[0].spec.ports[0].nodePort}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "200\n",
      "{\n",
      "  \"meta\": {\n",
      "    \"puid\": \"n0jtefhl4dbo8v25bntnkjuf03\",\n",
      "    \"tags\": {\n",
      "    },\n",
      "    \"routing\": {\n",
      "    },\n",
      "    \"requestPath\": {\n",
      "      \"complex-model\": \"model-with-bindata-rest:0.1\"\n",
      "    },\n",
      "    \"metrics\": [{\n",
      "      \"key\": \"mycounter\",\n",
      "      \"type\": \"COUNTER\",\n",
      "      \"value\": 1.0,\n",
      "      \"tags\": {\n",
      "      }\n",
      "    }, {\n",
      "      \"key\": \"mygauge\",\n",
      "      \"type\": \"GAUGE\",\n",
      "      \"value\": 100.0,\n",
      "      \"tags\": {\n",
      "      }\n",
      "    }, {\n",
      "      \"key\": \"mytimer\",\n",
      "      \"type\": \"TIMER\",\n",
      "      \"value\": 20.2,\n",
      "      \"tags\": {\n",
      "      }\n",
      "    }]\n",
      "  },\n",
      "  \"binData\": \"gANjbnVtcHkuY29yZS5tdWx0aWFycmF5Cl9yZWNvbnN0cnVjdApxAGNudW1weQpuZGFycmF5CnEBSwCFcQJDAWJxA4dxBFJxBShLAUsDSwOGcQZjbnVtcHkKZHR5cGUKcQdYAgAAAGk4cQhLAEsBh3EJUnEKKEsDWAEAAAA8cQtOTk5K/////0r/////SwB0cQxiiUNIAAAAAAAAAAABAAAAAAAAAAIAAAAAAAAAAwAAAAAAAAAEAAAAAAAAAAUAAAAAAAAABgAAAAAAAAAHAAAAAAAAAAgAAAAAAAAAcQ10cQ5iLg==\"\n",
      "}\n",
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "(3, 3)\n"
     ]
    }
   ],
   "source": [
    "bdata = get_payload()\n",
    "bdata_base64 = base64.b64encode(bdata).decode('utf-8')\n",
    "payload = {\"meta\":{},\"binData\":bdata_base64}\n",
    "response = rest_request_ambassador(\"mymodel\",payload,minikube_ip[0]+\":\"+minikube_port[0])\n",
    "bdata2 = base64.b64decode(response[\"binData\"])\n",
    "arr_resp = pickle.loads(bdata2)\n",
    "print(arr_resp)\n",
    "print(arr_resp.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "seldondeployment.machinelearning.seldon.io \"mymodel\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f deployment-rest.json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# gRPC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---> Installing application source...\n",
      "Build completed successfully\n"
     ]
    }
   ],
   "source": [
    "!eval $(minikube docker-env) && s2i build -E environment_grpc . seldonio/seldon-core-s2i-python36:0.4-SNAPSHOT model-with-bindata-grpc:0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "seldondeployment.machinelearning.seldon.io/mymodel created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f deployment-grpc.json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wait until ready (replicas == replicasAvailable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "map[predictorStatus:[map[name:mymodel-mymodel-a9ecaa4 replicas:1 replicasAvailable:1]] state:Available]"
     ]
    }
   ],
   "source": [
    "!kubectl get seldondeployments mymodel -o jsonpath='{.status}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "(3, 3)\n"
     ]
    }
   ],
   "source": [
    "bdata = get_payload()\n",
    "response = grpc_request_ambassador(\"mymodel\",bdata,minikube_ip[0]+\":\"+minikube_port[0])\n",
    "bdata2 = response.binData\n",
    "arr_resp = pickle.loads(bdata2)\n",
    "print(arr_resp)\n",
    "print(arr_resp.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl delete -f deployment-grpc.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!minikube delete"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
